package cc.wanforme.st.server.base.controller;


import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cc.wanforme.st.server.base.entity.UserRole;
import cc.wanforme.st.server.base.service.IUserRoleService;
import cc.wanforme.st.server.util.AssertCheck;
import cc.wanforme.st.server.vo.ResMessage;
import cc.wanforme.st.server.vo.base.UserRoleSimpleVo;
import cc.wanforme.st.server.vo.base.UserRoleVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wanne
 * @since 2023-01-03
 */
@Controller
@RequestMapping("/base/user-role")
public class UserRoleController {

	@Autowired
	private IUserRoleService userRoleService;
	
	@PostMapping("/add")
	@PreAuthorize("hasAuthority('userRole:add')")
	@ResponseBody
	public ResMessage<UserRole> add(@RequestBody UserRole vo) {
		UserRole vo2 = userRoleService.saveVo(vo);
		return ResMessage.success(vo2);
	} 
	
	@PostMapping("/update")
	@PreAuthorize("hasAuthority('userRole:update')")
	@ResponseBody
	public ResMessage<Void> update(@RequestBody UserRole vo) {
		userRoleService.updateVo(vo);
		return ResMessage.success();
	}
	
	@PostMapping("/updateRoles")
	@PreAuthorize("hasAuthority('userRole:update')")
	@ResponseBody
	public ResMessage<Void> updateRoles(@RequestBody UserRoleVo vo) {
		userRoleService.updateUserRoles(vo);
		return ResMessage.success();
	}
	
	@PostMapping("/del/{id}")
	@PreAuthorize("hasAuthority('userRole:delete')")
	@ResponseBody
	public ResMessage<Void> delete(@PathVariable String id) {
		userRoleService.deleteVo(id);
		return ResMessage.success();
	}
	
//	/** 角色模糊搜索
//	 * @param queryVo
//	 * @return
//	 */
//	@PostMapping("/list")
//	@ResponseBody
//	public ResMessage<PageInfo<RolePermission>> list(@RequestBody SearchQueryVo<String> queryVo) {
//		PageInfo<RolePermission> result = rolePermissionService.list (queryVo.getPage(),
//				queryVo.getSize(), queryVo.getParam());
//		return ResMessage.success(result);
//	}
	
	@PostMapping("/roles")
	@ResponseBody
	public ResMessage<UserRoleVo> roles(@RequestBody UserRoleSimpleVo vo ) {
		AssertCheck.nonNull(vo.getUserId(), "未知用户");
		UserRoleVo roles = userRoleService.selectUserAndRoles(vo.getUserId());
		return ResMessage.success(roles);
	}
	
}
